package com.zhongxiangspace.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhongxiangspace.entity.ArticleType;
import com.zhongxiangspace.entity.Articles;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.result.PageResult;
import com.zhongxiangspace.service.ArticleService;
import com.zhongxiangspace.service.OfficeBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class NewsListController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private OfficeBuildingService officeBuildingService;

    @GetMapping("lists/news")
    public String news(Model model) {
        List<ArticleType> articleTypeList = this.articleService.queryType(1);
        return news(model, articleTypeList.get(0).getId(), 1);
    }

    @GetMapping("lists/news{id}/pv{page}")
    public String news(Model model, @PathVariable("id") Integer id, @PathVariable("page") Integer page) {

        Map<String, Object> map = new HashMap<>();
        List<ArticleType> articleTypeList = this.articleService.queryType(1);
        map.put("articleTypeList", articleTypeList);
        QueryAnalyzer queryAnalyzer = new QueryAnalyzer();
        if (id == null) {
            id = articleTypeList.get(0).getId();
        }
        model.addAttribute("type_id",id);
        queryAnalyzer.setArticle_type_id(id);
        queryAnalyzer.setShowCode(1);
        PageHelper.startPage(page, 16, "articles.modify_time  DESC");
        Page<Map<String, Object>> articlesList = (Page<Map<String, Object>>) this.articleService.articlesAll(queryAnalyzer);
        PageResult pageResult=new PageResult(articlesList.getPageNum(),articlesList.getPageSize(),articlesList.getTotal(),articlesList.getPages(),articlesList.getResult(),true);
        model.addAttribute("pageResult", pageResult);
        //热门资讯
        QueryAnalyzer hot = new QueryAnalyzer();
        hot.setHotCode(1);
        hot.setShowCode(1);
        PageHelper.startPage(1, 10, "articles.modify_time  DESC");
        List<Map<String, Object>> hotArticles = this.articleService.articlesAll(hot);
        model.addAttribute("hotArticles", hotArticles);
        //热门楼盘
        QueryAnalyzer buildingQueryAnalyzer = new QueryAnalyzer();
        buildingQueryAnalyzer.setShowCode(1);
        buildingQueryAnalyzer.setHotCode(1);
        List<Map<String, Object>> officeBuildingsList = this.officeBuildingService.queryOfficeBuilding(buildingQueryAnalyzer);
        model.addAttribute("officeBuildings", officeBuildingsList);
        model.addAttribute("data", map);
        return "list/news_list";
    }
}
